<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物车</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      v-show="isTabFixed"
      class="tab-control">
    </tab-control>
    <scroll 
      class="content" 
      ref="scroll" 
      :probe-type="3" 
      :pull-up-load="true"
      @scroll="contentScroll" 
      @pullingUp="loadMore">
      <!-- 轮播图 -->
      <home-swiper :banners="banners" @swiperImgLoad="swiperImgLoad"></home-swiper>
      <!-- 推荐 -->
      <home-recommend :recommends="recommends"></home-recommend>
      <home-feature></home-feature>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2">
      </tab-control>
      <!-- 商品列表 -->
      <goods-list :goods="goods[currentType].list"></goods-list>
    </scroll>
    <!-- 回到顶部 -->
    <back-top @click.native="backClick" v-show="isShowBackTop"/>

  </div>
</template>

<script>
import HomeSwiper from "./childComps/HomeSwiper";
import HomeRecommend from "./childComps/HomeRecommend";
import HomeFeature from "./childComps/HomeFeature";

import NavBar from "@/components/common/navbar/NavBar";
import GoodsList from "@/components/content/goods/GoodsList";
import TabControl from "@/components/content/tabControl/TabControl";
import Scroll from "@/components/common/scroll/Scroll";
import BackTop from "@/components/content/backTop/BackTop"

import { getHomeMultidata, getHomeGoods } from "@/network/home";

export default {
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommend,
    HomeFeature,
    TabControl,
    GoodsList,
    Scroll,
    BackTop
  },
  data() {
    return {
      // 轮播图
      banners: [],
      recommends: [],
      // 首页商品信息
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      // 存储点击时的类型
      currentType: "pop",
      // 是否显示回到顶部按钮
      isShowBackTop: false,
      // tabControl的offsetTop值
      tabOffsetTop: 0,
      // tabControl是否吸顶
      isTabFixed: false,
      // 存储y值
      saveY: 0,
      itemImgListener: null
    };
  },
  created() {
    // 获取首页数据
    this.getHomeMultidata();
    // 获取首页商品信息
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  methods: {
    /*
        事件监听相关的方法
      */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
      }
      // 让两个tabControl的选项统一
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    // 回到顶部方法
    backClick() {
      this.$refs.scroll.scroll.scrollTo(0, 0, 500)
    },
    // 上拉加载更多
    loadMore() {
      this.getHomeGoods(this.currentType)
      // console.log("上拉加载更多");
    },
    // 防抖函数
    debounce(func, delay) {
      let timer = null
      return function(...args) {
        if (timer) clearTimeout(timer)
        timer = setTimeout(()=>{
          func.apply(this, args)
        }, delay) 
      }
    },
    // 轮播图加载完成
    swiperImgLoad() {
      // 获取tabControl的offsetTop值
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },
    /*
        网络请求相关的方法
      */
    // 获取首页数据
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.data.banner.list;
        this.recommends = res.data.data.recommend.list;
      });
    },
    // 获取首页商品信息
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.data.list);
        this.goods[type].page += 1;
        // 等数据加载完成后，再次开启下拉加载更多
        this.$refs.scroll.scroll.finishPullUp()
      });
    },
    // 获取滚动位置
    contentScroll(position) {
      // 判断返回顶部是否显示
      this.isShowBackTop = position.y < -1000
      // 决定tabControl是否吸顶
      this.isTabFixed = (-position.y) >= this.tabOffsetTop
    }
  },
  mounted() {
    // 监听事件总线的图片加载方法
    const refresh = this.debounce(this.$refs.scroll.refresh, 1000)
    this.itemImgListener = ()=>{
      refresh()
    }
    this.$bus.$on("itemImgLoad", this.itemImgListener)
  },
  activated() {
    this.$refs.scroll.refresh()
    this.$refs.scroll.scroll.scrollTo(0, this.saveY, 0)
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getCurrentY()

    // 取消全局事件
    this.$bus.$off('itemImgLoad', this.itemImgListener)
  },
  
};
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  font-size: var(--font-size);
  /* position: fixed;
  left: 0;
  right: 0;
  top: 0; */
  /* z-index: 1; */
}
.content {
  height: calc(100vh - 98px);
  overflow: hidden
}
</style>
